from django.db import models
from django.contrib.contenttypes.models import ContentType

class Aggregate(models.Model):
    """
    The data structure which links arbitrary objects, based on the
    uniqueness of a certain field, to a counter and a user_defined
    aggregate.
    """
    # The actual aggregation-related fields
    count = models.PositiveIntegerField(default=0)
    user_defined = models.IntegerField(default=0)
    # The fields which link each aggregate to other objects.
    content_type = models.ForeignKey(ContentType)
    unique_field = models.CharField(maxlength=100)
    unique_field_data = models.CharField(maxlength=100)

    class Meta:
        verbose_name = "Aggregate"
        verbose_name_plural = "Aggregates"
        unique_together = (('content_type', 'unique_field', 'unique_field_data'),)

    class Admin:
        pass

    def __unicode__(self):
        return u"%d %s object(s) with %s of %s (user-defined property: %d)" % (
            self.count, 
            self.content_type.name,
            self.unique_field,
            self.unique_field_data,
            self.user_defined,
        )